﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace An_Im.Model.QuickHelper
{
    /// <summary>
    /// 自动命令的基类。
    /// </summary>
    abstract class AutoCommandBase
    {
        /// <summary>
        /// 命令的参数。
        /// </summary>
        public IReadOnlyList<string> Params;

        /// <summary>
        /// 执行命令。
        /// </summary>
        public virtual void Execute()
        {
        }

        public AutoCommandBase(IEnumerable<string> paralist)
        {
            Params = new List<string>(paralist);
        }

    }

    /// <summary>
    /// 命令组。
    /// </summary>
    class GroupCommand : AutoCommandBase
    {
        private List<AutoCommandBase> _childCommandList;
        public IReadOnlyList<AutoCommandBase> ChildCommandList => _childCommandList;

        /// <summary>
        /// 命令组的循环次数。
        /// </summary>
        public int RepeatCount { get; set; } = 1;

        public GroupCommand() : base(new string[] { })
        {
            _childCommandList = new List<AutoCommandBase>();
        }


        /// <summary>
        /// 执行。
        /// </summary>
        public override void Execute()
        {
            for (int i = 0; i < RepeatCount; i++)
            {
                foreach (var command in ChildCommandList)
                {
                    command.Execute();
                }
            }
        }


        public void AddCommand(AutoCommandBase autoCommand)
        {
            _childCommandList.Add(autoCommand);
        }

        public void RemoveCommand(AutoCommandBase autoCommand)
        {
            _childCommandList.Remove(autoCommand);
        }

        public void AddCommandRange(IEnumerable<AutoCommandBase> autoCommands)
        {
            _childCommandList.AddRange(autoCommands);
        }

        public override string ToString()
        {
            return $"命令组 重复次数：{RepeatCount} 组内成员：{Params.Count}";
        }
    }


    /// <summary>
    /// 绝对移动命令
    /// </summary>
    class MoveAbsCommand : AutoCommandBase
    {
        public MoveAbsCommand(int x, int y) : base(new string[] { x.ToString(), y.ToString() })
        {

        }
        public override void Execute()
        {
            var x = int.Parse(this.Params[0]);
            var y = int.Parse(this.Params[1]);
            CursorHelper.SetCursorPositionAbs(x, y);
            base.Execute();
        }

        public override string ToString()
        {
            return $"鼠标绝对移动 X {this.Params[0]} Y {this.Params[1]}";
        }
    }

    /// <summary>
    /// 相对移动命令
    /// </summary>
    class MoveRelCommand : AutoCommandBase
    {
        public MoveRelCommand(int dx, int dy) : base(new string[] { dx.ToString(), dy.ToString() })
        {

        }
        public override void Execute()
        {
            var dx = int.Parse(this.Params[0]);
            var dy = int.Parse(this.Params[1]);
            CursorHelper.SetCursorPositionRel(dx, dy);
            base.Execute();
        }

        public override string ToString()
        {
            return $"鼠标相对移动 dX {this.Params[0]} dY {this.Params[1]}";
        }

    }

    class ClickCommand : AutoCommandBase
    {
        public ClickCommand(MouseButton mouseButton) : base(new string[] { mouseButton.ToString() })
        {

        }
        public override void Execute()
        {
            var mouseButton = (MouseButton)Enum.Parse(typeof(MouseButton), Params[0]);
            CursorHelper.Click(mouseButton);
            base.Execute();
        }

        public override string ToString()
        {
            return $"鼠标单击 按键 {this.Params[0]}";
        }
    }

    class PressCommand : AutoCommandBase
    {
        public PressCommand(MouseButton mouseButton) : base(new string[] { mouseButton.ToString() })
        {

        }
        public override void Execute()
        {
            var mouseButton = (MouseButton)Enum.Parse(typeof(MouseButton), Params[0]);
            CursorHelper.Click(mouseButton);
            base.Execute();
        }
        public override string ToString()
        {
            return $"鼠标按住 按键 {this.Params[0]}";
        }
    }

    class ReleaseCommand : AutoCommandBase
    {
        public ReleaseCommand(MouseButton mouseButton) : base(new string[] { mouseButton.ToString() })
        {

        }
        public override void Execute()
        {
            var mouseButton = (MouseButton)Enum.Parse(typeof(MouseButton), Params[0]);
            CursorHelper.Click(mouseButton);
            base.Execute();
        }
        public override string ToString()
        {
            return $"鼠标释放 按键 {this.Params[0]}";
        }
    }

    /// <summary>
    /// 延时命令。
    /// </summary>
    class SleepCommand : AutoCommandBase
    {
        public SleepCommand(double seconds) : base(new string[] { seconds.ToString() })
        {

        }
        public override void Execute()
        {
            var seconds = Double.Parse(Params[0]);
            var start = DateTime.Now;
            var end = start.Add(TimeSpan.FromSeconds(seconds));

            var temp = start;
            while (temp < end)
            {
                temp = DateTime.Now;
            }

            base.Execute();
        }

        public override string ToString()
        {
            return $"等待 时间 {this.Params[0]}秒";
        }

    }



}
